package classic150;

import java.util.Iterator;
import java.util.Objects;

/**
 * <h3>leetcode</h3>
 *
 * @author BigFaceCat
 * @date 2023/12/15 21:08
 * @description <p>leetCode383赎金信-hash表-简单</p>
 */
public class No383 {
    public static void main(String[] args) {
        String ransomNote = "aa";
        String magazine = "aab";
        System.out.println(canConstruct(ransomNote, magazine));

    }

    /**
     * 如果r能由m里面的字符构成，且m里面的字符只能在r里面出现一次
     * 那么说明m的长度大于等于r的长度
     */
    public static boolean canConstruct(String ransomNote, String magazine) {

        //m长度小于r，说明r不能全由m组成
        if (magazine.length() < ransomNote.length()){
            return false;
        }

//        String[] stringM = magazine.split("");
        String[] stringR = ransomNote.split("");
        for (String s : stringR) {
            if (magazine.contains(s)){
                magazine = magazine.replace(s, "");
//                System.out.println(s);
//                System.out.println(magazine);
            }

            if (magazine.length() == 0){
                return false;
            }
        }

        return false;
    }
}
